package lichtruong.bk.cse;

import java.util.ArrayList;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class ShowAllReported extends Activity{
	int  leveluser;
	GoogleMap googleMap;
	LatLng POINT_SHOW;
	Intent allInformation;
	ArrayList<PR> DataReported = new ArrayList<PR>();
	String[] allId, allInfo, allLat, allLng, allNumr, allTimenear, allMessagebl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map1);
		googleMap = ((MapFragment) getFragmentManager().findFragmentById(
				R.id.map)).getMap();
		allInformation = getIntent();
		allId = (allInformation.getStringExtra("allId")).split("-");
		allInfo = (allInformation.getStringExtra("allInfo")).split("-");
		allLat = (allInformation.getStringExtra("allLat")).split("-");
		allLng = (allInformation.getStringExtra("allLng")).split("-");
		allNumr = (allInformation.getStringExtra("allNumr")).split("-");
		allTimenear = (allInformation.getStringExtra("allTimenear")).split("-");
		allMessagebl = (allInformation.getStringExtra("allMessagebl")).split("-");
		
		
		// draw all point reported
		DrawAllPointReported();
		
		// zoom point choose view
		int index_show = Integer.parseInt(allInformation.getStringExtra("index"));
		POINT_SHOW = new LatLng(Double.parseDouble(allLat[index_show + 1]), 
								Double.parseDouble(allLng[index_show + 1]));
		googleMap.moveCamera(CameraUpdateFactory
				.newLatLngZoom(POINT_SHOW, 15));
		googleMap.animateCamera(
				CameraUpdateFactory.zoomTo(16),
				2000, null);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		super.onOptionsItemSelected(item);
		optionSelect();
		return true;
	}
	
	private void optionSelect(){
		String login;
		Database getlevel = new Database(ShowAllReported.this);
		getlevel.open();
		if(Integer.parseInt(getlevel.getData_USER().level) == 0){
			login = "                 Đăng Nhập     ";
			leveluser = Integer.parseInt(getlevel.getData_USER().level);
		}else{
			login = "                 Đăng Xuất     ";
			leveluser = Integer.parseInt(getlevel.getData_USER().level);
		}
		getlevel.close();
		CharSequence[] items = {"                   Đăng Ký ", login,"               Xem Tự Động   ", 
								"          Báo Cáo Tắc Đường   ", "     Xem Các Điểm Tắc Đường  "};
		new AlertDialog.Builder(this)
		.setTitle("   Danh Sách Lựa Chọn  ")
		.setItems(items, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if(which == 0){
					Intent register = new Intent(ShowAllReported.this, Register.class);
					startActivity(register);
				}else if(which == 1){
					if(leveluser == 0){
						Intent login = new Intent(ShowAllReported.this, Login.class);
						login.putExtra("back", "1");
						startActivity(login);
					}else{
						Database updateLevel = new Database(ShowAllReported.this);
						updateLevel.open(); updateLevel.updateLevelUser("0"); updateLevel.close();
						Toast.makeText(getBaseContext(), "Đăng Xuất Thành Công", Toast.LENGTH_SHORT).show();
						Intent menu = new Intent(ShowAllReported.this, menu.class);
						startActivity(menu);
					}
				}else if(which == 2){
					Intent autoget = new Intent(ShowAllReported.this, AuToGet.class);
					startActivity(autoget);
				}else if(which == 3){
					if(leveluser == 0){
						Intent login = new Intent(ShowAllReported.this, Login.class);
						login.putExtra("back", "1");
						startActivity(login);
					}else{
						Intent report = new Intent(ShowAllReported.this, Report.class);
						startActivity(report);
					}
				}else{
					Intent reported = new Intent(ShowAllReported.this, GetDataReported.class);
					startActivity(reported);
				}
			}
		}).show();
		//.setMessage(message)
	}
	
	public void DrawAllPointReported(){
		
		for(int i = 1; i < allId.length; i++){
			POINT_SHOW = new LatLng(Double.parseDouble(allLat[i]), 
					Double.parseDouble(allLng[i]));
			googleMap.addMarker(new MarkerOptions()
			.position(POINT_SHOW)
			.title(allInfo[i])
			.draggable(true)
			.icon(BitmapDescriptorFactory
					.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
			googleMap.addCircle(new CircleOptions()
			.center(POINT_SHOW).radius(200).strokeColor(Color.BLUE)
			.fillColor(0x40ff0000))
			.setStrokeWidth(2);
		}
	}
	
}